/**
 *
 */
package com.dhcc.frame.guava.functional;

import io.github.benas.jpopulator.api.Populator;
import io.github.benas.jpopulator.impl.PopulatorBuilder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhcc.frame.guava.entity.City;
import com.dhcc.frame.guava.entity.State;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;

/**
 * @author 谢进军
 *
 */
public class FunctionTest {
	private static final Logger logger = LoggerFactory.getLogger( FunctionTest.class );

	private Map<String, State> stateMap = new HashMap<>();

	private Populator populator = new PopulatorBuilder().build();
	@SuppressWarnings("unchecked")
	@Before
	public void setUp() throws Exception {
		List<State> states = populator.populateBeans(State.class);
		List<City> cities = populator.populateBeans( City.class, 3 );
		for( int i = 0;i< states.size();i++ ){
			states.get(i).setMainCites( new HashSet<City>(cities)  );
			stateMap.put( "State" + String.valueOf(i), states.get(i) );
		}
	}
	public void test1(){
		Function<Date, String> function = new Function<Date, String>() {
			@Override
			public String apply(Date input) {
				return new SimpleDateFormat("dd/mm/yyyy").format( input );
			}
		};
	}
	//------------Functions类
	@org.junit.Test
	public void testForMap(){
//		Function<String, State> lookup = Functions.forMap( stateMap );
		Function<String, State> lookup = Functions.forMap( stateMap,null );
		State state = lookup.apply( "State4" );
		logger.info( ToStringBuilder.reflectionToString(state,ToStringStyle.MULTI_LINE_STYLE ) );
	}

	@org.junit.Test
	public void testCompose(){
		Function<String, State> lookup = Functions.forMap( stateMap,null );
		Function<State, String> stateFunction = new Function<State, String>() {
			@Override
			public String apply(State input) {
				return Joiner.on(",").join(input.getMainCites());
			}

		};
		Function<String, String> composed = Functions.compose( stateFunction, lookup );
		String result = composed.apply( "State4" );
		System.out.println( result );
	}
}
